package cn.kgc.layui.servlet;

import cn.kgc.layui.entity.Authority;
import cn.kgc.layui.entity.Student;
import cn.kgc.layui.service.AuthorityService;
import cn.kgc.layui.service.impl.AuthorityServiceImpl;
import cn.kgc.layui.util.LayUiResult;
import cn.kgc.layui.vo.ZtreeVO;
import com.alibaba.fastjson.JSON;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Enumeration;
import java.util.Map;

@WebServlet("/authority/*")
public class AuthorityServlet extends BaseServlet<Authority> {
    AuthorityService authorityService=new AuthorityServiceImpl();

    /**
     * 查询 操作
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void list(HttpServletRequest req, HttpServletResponse resp,Authority auth) throws ServletException, IOException {
        // 获取配置 page limit
        String authorityName = req.getParameter("authorityName");
        String menuUrl = req.getParameter("menuUrl");
        String authority = req.getParameter("authority");
        LayUiResult<Authority> result =authorityService.list(authorityName,menuUrl,authority);
        // 以Json类型数据的格式响应数据
        String strResult = JSON.toJSONString(result);
        resp.getWriter().write(strResult);
    }

    public void ztree(HttpServletRequest req, HttpServletResponse resp,Authority auth) throws IOException {
        String roleId=req.getParameter("roleId");
        LayUiResult<ZtreeVO> result =authorityService.ztreeData(roleId);

        // 以Json类型数据的格式响应数据
        String strResult = JSON.toJSONString(result);
        resp.getWriter().write(strResult);
    }

    /**
     * 新增操作
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void add(HttpServletRequest req, HttpServletResponse resp,Authority auth) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {

        System.out.println(auth.toString());

        String authorityName = req.getParameter("authorityName");
        String menuUrl = req.getParameter("menuUrl");

        int parentId = req.getParameter("parentId").length()>0?Integer.parseInt(req.getParameter("parentId")):-1;

        int isMenu = Integer.parseInt(req.getParameter("isMenu"));
        String menuIcon = req.getParameter("menuIcon");
        int orderNumber = Integer.parseInt(req.getParameter("orderNumber"));
        String authority = req.getParameter("authority");


        int result=authorityService.save(authorityName,menuUrl,parentId,isMenu,menuIcon,orderNumber,authority);
        LayUiResult layUiResult;
        if(result>0){
            layUiResult=LayUiResult.builder().code(200).msg("保存成功").build();
        }else {
            layUiResult=LayUiResult.builder().code(500).msg("保存失败").build();
        }

        // 以Json类型数据的格式响应数据
        String strResult = JSON.toJSONString(layUiResult);
        resp.getWriter().write(strResult);

    }

    /**
     * 修改
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void update(HttpServletRequest req, HttpServletResponse resp,Authority auth) throws ServletException, IOException{

        String authorityName = req.getParameter("authorityName");
        String menuUrl = req.getParameter("menuUrl");

        int parentId = req.getParameter("parentId").length()>0?Integer.parseInt(req.getParameter("parentId")):-1;

        int isMenu = Integer.parseInt(req.getParameter("isMenu"));
        String menuIcon = req.getParameter("menuIcon");
        int orderNumber = Integer.parseInt(req.getParameter("orderNumber"));
        int authorityId = Integer.parseInt(req.getParameter("authorityId"));
        String authority = req.getParameter("authority");


        int result=authorityService.update(authorityId,authorityName,menuUrl,parentId,isMenu,menuIcon,orderNumber,authority);
        LayUiResult layUiResult;
        if(result>0){
            layUiResult=LayUiResult.builder().code(200).msg("修改成功").build();
        }else {
            layUiResult=LayUiResult.builder().code(500).msg("修改失败").build();
        }

        // 以Json类型数据的格式响应数据
        String strResult = JSON.toJSONString(layUiResult);
        // 通过响应对象获取输出流
        resp.getWriter().write(strResult);
    }

    public void delete(HttpServletRequest req, HttpServletResponse resp,Authority auth) throws ServletException, IOException{
        // 获取前端传递的参数
        String id= req.getParameter("id");
        String ids= req.getParameter("ids");
        int result=authorityService.delete(id,ids);
        LayUiResult layUiResult;
        if(result>0){
            layUiResult=LayUiResult.builder().code(200).msg("删除成功").build();
        }else {
            layUiResult=LayUiResult.builder().code(500).msg("删除失败").build();
        }
        // 以Json类型数据的格式响应数据
        String strResult = JSON.toJSONString(layUiResult);
        resp.getWriter().write(strResult);
    }
}
